package com.sooncode.project.focusservice;

import com.sooncode.project.core.model.Creater;
import com.sooncode.project.core.monitor.Monitor;
import com.sooncode.project.focusservice._.infrastructure.RepositoryFactory;
import com.sooncode.project.springcloudcommon.model.AuthInfo;
import com.sooncode.project.springcloudcommon.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class Configuration {
    @Autowired
    HttpServletRequest request;
    @Autowired
    public Configuration(RepositoryFactory factory){
        Monitor monitor=Monitor.New();
        monitor.ConfigCreater(()->{
            try {
                AuthInfo currentUser = UserUtils.getCurrentUser();
                if (currentUser == null) return new Creater("", "", request.getHeader("x-forwarded-for"));
                return new Creater(currentUser.getName(), currentUser.getId(),request.getHeader("x-forwarded-for"));
            } catch (Exception ex) {
                return null;
            }
        });
        monitor.ConfigDomainRepository(factory.getRepository());
        monitor.RegisterLookupModel("com.sooncode.project.focusservice.model");
    }
}
